package com.aiju.cash.ui.member;

import android.os.Bundle;
import android.text.InputFilter;
import android.text.InputType;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

import com.aiju.base.activity.BaseTitleActivity;
import com.aiju.cash.R;
import com.aiju.cash.core.api.DataJson;
import com.aiju.cash.core.api.MemberApi;
import com.aiju.common.ActivityStack;
import com.aiju.forexpand.datepicker.DatePicker;
import com.aiju.utils.CheckUtil;
import com.aiju.utils.DateUtil;
import com.aiju.utils.KeyBoardUtils;
import com.aiju.utils.T;
import com.aiju.widgit.layout.SettingItemEditView;
import com.aiju.widgit.layout.SettingItemTextView;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;

/**
 * @author: 佐为
 * @date: 2017-02-20 15:59
 * @version: V1.0
 * @Description: 新建会员卡
 */
public class MemberCardAddActivity extends BaseTitleActivity implements OnClickListener {

    private SettingItemEditView nameEditText, discountEditText;

    private SettingItemTextView starTimeTextView, endTimeTextView;

    private EditText infoEditText;

    private String startDate = "2017-02-25";
    private String endDate = "2017-03-15";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_member_card_add);
        setCenterTitle("新建会员卡");

        nameEditText = (SettingItemEditView) findViewById(R.id.tv_member_name);
        discountEditText = (SettingItemEditView) findViewById(R.id.tv_member_discount);
        starTimeTextView = (SettingItemTextView) findViewById(R.id.tv_member_start);
        endTimeTextView = (SettingItemTextView) findViewById(R.id.tv_member_end);
        infoEditText = (EditText) findViewById(R.id.ev_member_info);

        starTimeTextView.setOnClickListener(this);
        endTimeTextView.setOnClickListener(this);
        findViewById(R.id.btn_good_add_save).setOnClickListener(this);
        findViewById(R.id.btn_good_add_save).setOnClickListener(this);

        /*starTimeTextView.setRightText(DateUtil.getCurrentDate(DateUtil.dateFormatYMD));
        endTimeTextView.setRightText(DateUtil.getCurrentDateByOffset(DateUtil.dateFormatYMD, Calendar.DATE, 30));*/
        nameEditText.getRightView().setHint("仅支持文字和数字，限10个字");
        discountEditText.getRightView().setHint("例如：7.8");
        discountEditText.getRightView().setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        nameEditText.setEditable(true);
        discountEditText.setEditable(true);
        discountEditText.getRightView().setFilters(new InputFilter[]{
                new InputFilter() {
                    @Override
                    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                        String oriValue = dest.toString();
                        StringBuffer sb = new StringBuffer(oriValue);
                        sb.append(source);
                        String newValue = sb.toString();

                        Double aDouble = Double.valueOf(newValue);
                        if (aDouble > 10 || newValue.length() > 4) {
                            return "";
                        }
                        return source;
                    }
                }
        });
    }

    /**
     * 新建会员卡
     */
    private void requestAddMember() {
        String name = nameEditText.getRightText();
        String discount = discountEditText.getRightText();
        String info = infoEditText.getText().toString();

        startDate = starTimeTextView.getRightString();
        endDate = endTimeTextView.getRightString();
        if (TextUtils.isEmpty(name)) {
            T.ToastShort("请输入会员卡名称");
            return;
        }
        if (!CheckUtil.checkMemberCardName(name)) {
            T.ToastShort("会员卡名称不合规，请重新输入");
            return;
        }
        if (TextUtils.isEmpty(discount)) {
            T.ToastShort("请输入折扣");
            return;
        }
        Double dis = Double.valueOf(discount);
        if (!(dis > 0 && dis < 10)) {
            T.ToastShort("折扣必须大于0，小于10");
            return;
        }
        if (TextUtils.isEmpty(startDate)) {
            T.ToastShort("请设置生效时间");
            return;
        }
        if (TextUtils.isEmpty(endDate)) {
            T.ToastShort("请设置失效时间");
            return;
        }
        if (!(DateUtil.compare_date(startDate, endDate) == -1)) {
            T.ToastShort("失效时间不能早于生效时间，请重新输入");
            return;
        }
        if (!(DateUtil.compare_date(DateUtil.getCurrentDate(DateUtil.dateFormatYMD), endDate) == -1)) {
            T.ToastShort("失效时间不能早于当前时间，请重新输入");
            return;
        }

        mDialogHelper.showLoadingProgressDialog();
        new MemberApi().memberCardAdd(mUser.getStoreId(), mUser.getToken(),
                name, discount, info, "2", startDate, endDate, "1", "1", new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        mDialogHelper.dismissProgressDialog();
                        T.ToastNetError();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        mDialogHelper.dismissProgressDialog();
                        DataJson dataJson = new DataJson(response);
                        if (dataJson.isSuccess()) {
                            T.ToastShort("会员卡创建成功");
                            ActivityStack.finish(ActivityStack.findActivity(MemberCardActivity.class.getName()));
                            startActivity(MemberCardActivity.class);
                        } else {
                            T.ToastShort(dataJson.getMsg());
                        }
                    }
                });
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        KeyBoardUtils.closeKeybord(((EditText) nameEditText.getRightView()), this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_member_start:
                showBirthday(starTimeTextView.getRightView(), "开始时间");
                break;
            case R.id.tv_member_end:
                showBirthday(endTimeTextView.getRightView(), "截止时间");
                break;
            case R.id.btn_good_add_save:
                requestAddMember();
                break;
        }
    }

    /**
     * 选择生日
     */
    private void showBirthday(TextView textView, String title) {
        DatePicker dataPicker = new DatePicker(this);
        dataPicker.selectDateDialog(textView, DateUtil.getCurrentDate(DateUtil.dateFormatYMD));
        dataPicker.getTitle().setText(title);
    }
}
